package com.qfedu.Action;

import com.qfedu.anno.LoginAnno;
import com.qfedu.biz.AddressBiz;
import com.qfedu.biz.OrderBiz;
import com.qfedu.biz.OrderDetatilBiz;
import com.qfedu.entity.*;
import com.qfedu.task.OrderTask;
import com.qfedu.util.Ajaxd;
import org.apache.tools.ant.taskdefs.condition.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class OrderAction {
@Autowired
private OrderBiz orderBiz;
@Autowired
private AddressBiz addressBiz;
@Autowired
private OrderDetatilBiz orderDetatilBiz;
@RequestMapping("/toAddress.to")
@LoginAnno
public String toaddress(String[] evecheck, String type, HttpServletRequest request){
    request.setAttribute("type",type);
    request.setAttribute("evecheck",evecheck);
    //查询当前用户地址信息
    CustomerEntity customerinfo = (CustomerEntity)request.getSession().getAttribute("customerinfo");
    AddressEntity addressEntity = new AddressEntity();
    addressEntity.setCid(customerinfo.getCid());
    List<AddressEntity> addressEntities = addressBiz.queryAddressByCid(addressEntity);
    request.setAttribute("addresslist",addressEntities);
    return "address";
}

//生成订单和订单详情
    @RequestMapping("/orderinsert.do")
    public String  doOrderInsert(String[] evecheck, String type,String addressid,AddressEntity addressEntity, HttpServletRequest request){
        HttpSession session = request.getSession();
        Map<String, GoodsEntity> shopCar = (Map<String, GoodsEntity>)request.getSession().getAttribute("shopCar");
        float allprice =0;

        CustomerEntity customerinfo = (CustomerEntity)session.getAttribute("customerinfo");
        int cid=customerinfo.getCid();

        if (type!=null && type!=""){

            Set<String> strings = shopCar.keySet();
            evecheck = strings.toArray(new String[strings.size()]);
        }

        OrderDetailEntity orderDetailEntity = new OrderDetailEntity();
        List<GoodsEntity> goodslist=new ArrayList<>();
        for (String gid:evecheck){
            GoodsEntity goodsEntity = shopCar.get(gid);
            allprice+=goodsEntity.getGprice()*goodsEntity.getGoodscount()*goodsEntity.getGdiscount();
            goodslist.add(goodsEntity);
        }
//        request.setAttribute("orderdetail",goodslist);

        //添加地址
        if (addressEntity.getAddress()!=null&& addressEntity.getAddress()!=""){
            addressEntity.setCid(cid);
            Boolean aBoolean = addressBiz.insertAddress(addressEntity);
        }else{
            int addid = Integer.parseInt(addressid);
            addressEntity=new AddressEntity();
            addressEntity.setAddressid(addid);
            List<AddressEntity> addressEntities = addressBiz.queryAddressByCid(addressEntity);
            addressEntity=addressEntities.get(0);
        }


        //生成订单sq
        OrderEntity orderEntity = orderBiz.insertOrder(allprice, cid, addressEntity.getAddressid());
        //设置定时器，一定时间内未支付，取消订单
        //为什么要将定时器传入定时任务中呢？因为定时器是一个单线程，实现了runnable接口，在取消定时器时可能任务还未执行
        Timer timer = new Timer();
        //为定定时器添加任务，20秒后执行
        timer.schedule(new OrderTask(orderBiz,orderEntity.getOid(),timer),20000);
        request.getServletContext().setAttribute("timer",timer);
        List<OrderEntity> orderEntities = orderBiz.queryOrderById(orderEntity);
        orderEntity=orderEntities.get(0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
        String format = simpleDateFormat.format(orderEntity.getOcreateDate());
        request.setAttribute("format",format);
        request.setAttribute("order",orderEntity);


        //生成订单详情

        Boolean aBoolean = orderDetatilBiz.insertOrderDetail(goodslist,orderEntity.getOid());



        return "order";
    }

    //订单详情查询
    @RequestMapping("/queryOrderDetail.do")
    @ResponseBody
    public List<OrderDetailEntity> doqueryDetail(String oid){

        List<OrderDetailEntity> orderDetailEntities = orderDetatilBiz.queryOrderDetail(oid);
        return orderDetailEntities;
    }


    //订单查询
    @RequestMapping("/queryMyOrder.do")
    public String doQueryAllorder(HttpServletRequest request){
        HttpSession session = request.getSession();
        CustomerEntity customer =(CustomerEntity)session.getAttribute("customerinfo");
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setCid(customer.getCid());
        List<OrderEntity> orderEntities = orderBiz.queryOrderById(orderEntity);
        request.setAttribute("myorders",orderEntities);
        return "myorder";
    }

    //订单支付状态详情跳转
    @RequestMapping("/orderdetail.to")
    public String toorder(OrderEntity orderEntity,HttpServletRequest request){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
        List<OrderEntity> orderEntities = orderBiz.queryOrderById(orderEntity);
        orderEntity=orderEntities.get(0);
        String format = simpleDateFormat.format(orderEntity.getOcreateDate());
        request.setAttribute("format",format);
        request.setAttribute("order",orderEntity);
        return "order";
    }

    //详情页面跳转
    @RequestMapping("/qurtyOrderDetail.to")
    public String toorderdetail(String oid,HttpServletRequest request){
    request.setAttribute("oid",oid);
    return "orderDetail";
    }


    //订单结算,取消定时任务
    @RequestMapping("orderOver.do")
    public String doOrderover(String oid,HttpServletRequest request){
        Boolean flag = orderBiz.updataStatus(oid,2);
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOid(oid);
        List<OrderEntity> orderEntities = orderBiz.queryOrderById(orderEntity);
        orderEntity=orderEntities.get(0);
        request.setAttribute("order",orderEntity);
        if (flag){
            //拿出定时器取消定时任务
            Timer timer = (Timer)request.getServletContext().getAttribute("timer");
            timer.cancel();
            //从application域中去除timer的attribute属性
            request.getServletContext().removeAttribute("timer");
            return "order";
        }
        return "error";

    }
}
